package com.example.blog.controller.blog;

import com.example.blog.pojo.Comment;
import com.example.blog.pojo.Result;
import com.example.blog.service.CommentService;
import com.example.blog.util.TokenUtil.GetCookie;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/Comments")
public class CommentController {
    @Resource
    private CommentService commentService;

    //新增评论
    @PostMapping("/addComment/{id}")
    public Result saveComment(@RequestBody Comment comment, @PathVariable int id, HttpServletRequest request) {
        String cookieValue = request.getHeader("Cookie");
        String token = GetCookie.getCookieValue(cookieValue);
        return commentService.saveComment(comment, id, token);
    }

    //获取评论
    @GetMapping("/getComment/{id}")
    public Result getComment(@PathVariable int id) {
        return commentService.getComment(id);
    }
    //获取最新评论
    @GetMapping("/getNewComments")
    public Result<List<Comment>> getNewComment(){
        return commentService.getNewComment();
    }
}
